package com.tedu.weibo.controller;

import com.tedu.weibo.comment.result.JsonResult;
import com.tedu.weibo.comment.result.StatusCode;
import com.tedu.weibo.mapper.CommentMapper;
import com.tedu.weibo.pojo.dto.CommentDTO;
import com.tedu.weibo.pojo.entity.Comment;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Api(tags = "评论模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @ApiOperation("发表评论功能")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentDTO,comment);
            comment.setUserId(user.getId());
            comment.setCreated(new Date());
            commentMapper.insertComment(comment);
            //return new JsonResult(StatusCode.OPERATION_SUCCESS);
            return JsonResult.sucess();

        }
    }

    @ApiOperation("获取微博评论列表")
    @ApiImplicitParam(name = "id",value = "微博Id",required = true,example = "1")
    @GetMapping("/selectByWeiboId")
    public JsonResult selectByWeiboId(int id) {
        List<CommentVO> list=commentMapper.selectCommentByWid(id);
//        return new JsonResult(StatusCode.OPERATION_SUCCESS,list);
        return JsonResult.sucess(list);
    }


}
